/*
 *  HowlSet.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/06/17.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "HowlSet.h"
#include "../../sc/Common/WellTemperament.h"
#include <sstream>

namespace sc
{
namespace hs
{

	int HowlSet::FindIndexAtHowlFrequency( double frequency ) const
	{
		std::multiset< Howl >::const_iterator iter = begin();
		int index = 0;
		for ( ; iter != end(); iter++ )
//		for ( size_t i = 0; i < size(); i++ )
		{
			const Howl& howl = * iter;
			const double howlFreq = howl.GetFrequency();
			if ( fabs( frequency - howlFreq ) <= WellTemperament::calculationError )
			{
				return index;
			}
			index++;
		}
		return -1;
	}

	bool HowlSet::IsExistHowlFrequency( double frequency ) const
	{
		if ( FindIndexAtHowlFrequency( frequency ) == -1 )
		{
			return false;
		}
		return true;
	}

	std::string HowlSet::ToString( int tabCount, int harmonicNthMax ) const
	{
		std::stringstream ss;
		std::string tabs;
		std::multiset< Howl >::const_iterator iter = begin();
		for ( int i = 0; i < tabCount; i++ )
		{
			tabs.append( "\t" );
		}
		ss	<< tabs << "HowlSet:\n"
			<< tabs << "count: " << size() << "\n";
		for ( ; iter != end(); iter++ )
		{
			if ( ( iter->GetHarmonicSound( true ).GetNthOfHarmonic() > harmonicNthMax ) or
				 ( iter->GetHarmonicSound( false ).GetNthOfHarmonic() > harmonicNthMax ) )
			{
				continue;
			}

			std::string s = iter->ToString( tabCount + 1 );;
			if ( s == std::string( "" ) )
			{
				continue;
			}
			ss << s << "\n";
		}
		return ss.str(); 
	}


}
}